package com.letu.elasticsearch.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;

/**
 * es 配置
 */
@Configuration
public class EsConfig {
    /**
     * 自定义数据转换器
     * @param mappingContext
     * @return
     */
    @Bean
    ElasticsearchConverter elasticsearchConverter(SimpleElasticsearchMappingContext mappingContext) {
        DefaultConversionService defaultConversionService = new DefaultConversionService();
        defaultConversionService.addConverter(new DateToLong());
        defaultConversionService.addConverter(new LongToDate());
        defaultConversionService.addConverter(new StringToDate());
        return new MappingElasticsearchConverter(mappingContext, defaultConversionService);
    }
}
